package com.hyjx.framework.component.interfaceservice.dataservice;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.hyjx.framework.service.SessionManager;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.common.util.DataGrideUtil;
import com.hyjx.framework.common.util.MD5Helper;
import com.hyjx.framework.common.util.StringHelper;
import com.hyjx.framework.component.codetable.CodeTableManager;
import com.hyjx.framework.component.interfaceservice.ConditionModel;
import com.hyjx.framework.component.interfaceservice.InterFaceConstants;
import com.hyjx.framework.component.interfaceservice.UserInfoModel;
import com.hyjx.framework.component.mutidatasource.DynamicDataSource;
import com.hyjx.framework.component.mutidatasource.datasources.DataSourcesModel;
import com.hyjx.framework.orm.dao.Pager;

import net.sf.json.JSONObject;

/**
 * 数据服务控制器
 */
@Controller
@RequestMapping("/dataservice")
public class DataServiceController {
	@Resource
	private DataServiceService service;

	List<SrvConditionModel> list=new ArrayList<SrvConditionModel>();
	/**
	 * 到列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoList", method = RequestMethod.GET)
	public String gotoList(HttpServletRequest request) {
		return "/framework/interfaceservice/dataservice/dataservice_list";
	}

	/**
	 * 查询列表数据
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	@ResponseBody
	public String queryList(DataServiceModel model, HttpServletRequest request) {
		
		//list.add(new SrvConditionModel());
		Pager li=service.queryListA(model);
		for(int i=0;i<li.getResultList().size();i++){
			DataServiceModel temp=(DataServiceModel) li.getResultList().get(i);
			String codesrvname=CodeTableManager.getNameByCode("interface_srv_type", temp.getSrvtype());
			temp.setSrvtype(codesrvname);
		}
		/*StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"total\": "+li.size()+",");
		sb.append("\"rows\": ");
		sb.append(CommonUtil.getJsonString(li).toString());
		sb.append("}");*/
		
	
		return CommonUtil.getPageJsonStr(li);
	}

	/**
	 * 到新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoAdd", method = { RequestMethod.GET, RequestMethod.POST },produces = "application/json; charset=utf-8")
	public ModelAndView gotoAdd(HttpServletRequest request) {
		List list=service.serviceAllDs(null);
		DataSourcesModel basemodel=new DataSourcesModel();
		basemodel.setDatasourcename("dataSourceS");
		list.add(basemodel);
		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/dataservice_add");
		mav.addObject("list",list);
		return mav;
		//return "/framework/interfaceservice/dataservice/dataservice_add";
	}

	/**
	 * 保存对象
	 * 
	 * @param DataServiceModel
	 * @return
	 */
	@RequestMapping(value = "/doAdd", method = RequestMethod.POST)
	@ResponseBody
	public String doAdd(DataServiceModel model,HttpServletRequest request) {
		model.setSrvid(StringHelper.getPrimaryKey());
		model.setList(list);
		boolean con1 = service.add(model);
		boolean con2 = true;
		for (int i = 0; i < list.size(); i++) {
			SrvConditionModel m = list.get(i);
			m.setId(StringHelper.getPrimaryKey());
			// m.setId(StringHelper.getPrimaryKey());
			m.setSrvid(model.getSrvid());
			boolean con3 = service.addCondition(m);
			con2 = (con2 && con3);
		}
		//清理session里面的值
		list.clear();
		SessionManager.set("listSrvConditionModel", list);
		// 刷新SysConstants的缓存
		InterFaceConstants.setServiceConfig();
		InterFaceConstants.getServiceConfig();
		return "success";

	}

	/**
	 * 到修改页面 request
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoEdit", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView gotoEdit(HttpServletRequest request) {
		String id = request.getParameter("id");

		DataServiceModel qp = new DataServiceModel();
		SessionManager.set("srvid", id);

		qp.setSrvid(id);
		DataServiceModel model = (DataServiceModel) (service.getInfoById(qp));
		//找出所有的数据源
		List list=service.serviceAllDs(null);
		DataSourcesModel ds=new DataSourcesModel();
		ds.setDatasourcename("dataSourceS");
		list.add(ds);
		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/dataservice_edit");
		mav.addObject("model", model);
		mav.addObject("list", list);
		return mav;
	}

	/**
	 * 更新数据
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/doEdit", method = RequestMethod.POST)
	@ResponseBody
	public String doEdit(DataServiceModel model) {
		int temp=0;
		temp=service.editMy(model);
		if (temp > 0) {
			InterFaceConstants.setServiceConfig();
			InterFaceConstants.getServiceConfig();
			return "success";
		} else {
			return "failure";
		}
	}

	/**
	 * 删除 request
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoDelete", method = RequestMethod.GET)
	@ResponseBody
	public String gotoDelete(HttpServletRequest request) {
		String id = request.getParameter("ids");
		//这里判断该服务是是否已被使用，如使用则不允许删除，如果没使用就级联删除
		int count=service.condel(id);
		if(count>0){
			return "fail";
		}else if(count==0){
			//这里涉及到级联删除
			DataServiceModel qp = new DataServiceModel();
			qp.setSrvid(id);

			if (service.deleteClear(qp) > 0) {
				return "success";
			} else {
				return "failure";
			}
		}else{
			return "nothing";
		}
		
		
	}

	/**
	 * 到查看页面 request
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoView", method = RequestMethod.GET)
	public ModelAndView gotoView(HttpServletRequest request) {
		String id = request.getParameter("id");
		DataServiceModel qp = new DataServiceModel();
		qp.setSrvid(id);
		DataServiceModel model = (DataServiceModel) (service.getInfoById(qp));

		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/dataservice_view");
		mav.addObject("model", model);
		return mav;
	}

	/**
	 * 到新参数条件页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoParameterConditiontemp", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView gotoParameterConditiontemp(HttpServletRequest request, String srvid) {

		String srvid1 = request.getParameter("srvid");
		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/servicecondition");
		mav.addObject("srvid", srvid);
		return mav;
	}

	/**
	 * 查询参数条件列表数据
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryListForCondition", method = RequestMethod.POST)
	@ResponseBody
	public String queryListForCondition(String srvid, HttpServletRequest request) {
		return service.queryCondition(srvid);
	}

	/**
	 * 查询参数条件列表数据返回空值
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryListForConditionTemp")
	@ResponseBody
	public String queryListForConditionTemp(HttpServletRequest request) {
		List<SrvConditionModel> listtemp =new ArrayList<SrvConditionModel>();
		return DataGrideUtil.getDataGridJsonByList(listtemp);
		
	}
	
	
	/**
	 * 将保存的参数条件放入session中，然后再datagrid里面显示
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/showSessionCondition")
	@ResponseBody
	public String showSessionCondition(HttpServletRequest request) {
		List<SrvConditionModel> listtemp=(List<SrvConditionModel>) SessionManager.get("listSrvConditionModel");
//		StringBuffer sb = new StringBuffer();
//		sb.append("{");
//		sb.append("\"total\": "+listtemp.size()+",");
//		sb.append("\"rows\": ");
//		if(listtemp.size()>0){
//			sb.append(CommonUtil.getJsonString(listtemp).toString());
//		}
//		
//		sb.append("}");
		if(listtemp == null){
			listtemp = new ArrayList<SrvConditionModel>();
		}
		return DataGrideUtil.getDataGridJsonByList(listtemp);
		
		
	}
	
	
	
	/**
	 * 将保存的参数条件放入session中，然后再datagrid里面显示
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/delSessionCondition")
	@ResponseBody
	public String delSessionCondition(HttpServletRequest request) {
		String id = request.getParameter("ids");
		   
		//int index=Integer.parseInt(id);
		List<SrvConditionModel> listtemp=(List<SrvConditionModel>) SessionManager.get("listSrvConditionModel");
//		StringBuffer sb = new StringBuffer();
//		sb.append("{");
//		sb.append("\"total\": "+listtemp.size()+",");
//		sb.append("\"rows\": ");
//		if(listtemp.size()>0){
//			sb.append(CommonUtil.getJsonString(listtemp).toString());
//		}
//		
//		sb.append("}");
		if(listtemp != null){
			for(int i=0;i<listtemp.size();i++){
				SrvConditionModel temp=listtemp.get(i);
				if(id.equals(temp.getId())){
					listtemp.remove(i);
					return "success";
				}
			}
		}
		return "fail";
		
		
	}

	/**
	 * 删除参数条件 request
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoDeleteCondition", method = RequestMethod.GET)
	@ResponseBody
	public String gotoDeleteCondition(HttpServletRequest request) {
		String id = request.getParameter("ids");

		SrvConditionModel qp = new SrvConditionModel();
		qp.setId(id);
		// SrvConditionModel model =(SrvConditionModel)
		// (service.getInfoById(qp));
		if (service.deleteCondition(qp) > 0) {
			return "success";
		} else {
			return "failure";
		}
	}

	/**
	 * 到参数条件编辑页面添加条件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoAddParameterConditiontemp", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView gotoAddParameterConditiontemp(HttpServletRequest request, String srvid) {

		String srvid1 = request.getParameter("srvid");
		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/addservicecondition");
		mav.addObject("srvid", srvid);
		return mav;
	}

	/**
	 * 添加参数条件
	 * @param DataServiceModel
	 * @return
	 */
	@RequestMapping(value = "/doAddCondition")
	@ResponseBody
	public String doAddCondition(SrvConditionModel m,HttpServletRequest request) {
		
		
			m.setId(StringHelper.getPrimaryKey());
		
		
		String srvidtemp=m.getSrvid();
		if(srvidtemp.equals("null")){
			list.add(m);
			SessionManager.set("listSrvConditionModel", list);
			return "success";
		}else{
			boolean con2=false;
			
			con2=service.addCondition(m);
			if(con2){
				return "success";
			}else{
				return "failure";
			}
		}
	
			
	}

	/**
	 * 到参数条件编辑页面编辑条件
	 * 
	 * @return     gotoEditParameterCondition
	 */
	@RequestMapping(value = "/gotoEditParameterConditiontemp", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView gotoEditParameterConditiontemp(HttpServletRequest request, String id) {

		SrvConditionModel qp = service.queryConditionByid(id);
		// qp.setSrvid(id);

		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/editserviceconditionedit");
		mav.addObject("model", qp);
		return mav;
	}
	@RequestMapping(value = "/gotoViewParameterConditiontemp", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView gotoViewParameterConditiontemp(HttpServletRequest request, String id) {

		SrvConditionModel qp = service.queryConditionByid(id);
		// qp.setSrvid(id);

		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/editserviceconditionview");
		mav.addObject("model", qp);
		return mav;
	}
	
	
	/**
	 * 增加服务时编辑session里面的数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/gotoEditParameterConditionSession", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView gotoEditParameterConditionSession(HttpServletRequest request, String id) {
		List<SrvConditionModel> listtemp=(List<SrvConditionModel>) SessionManager.get("listSrvConditionModel");
		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/editservicecondition");

		for(int i=0;i<listtemp.size();i++){
			SrvConditionModel temp=listtemp.get(i);
			if(id.equals(temp.getId())){
				mav.addObject("model", temp);
				return mav;
			}
		}
		// qp.setSrvid(id);

		
		
		return mav;
	}
	
	@RequestMapping(value = "/gotoViewParameterConditionSession", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView gotoViewParameterConditionSession(HttpServletRequest request, String id) {
		List<SrvConditionModel> listtemp=(List<SrvConditionModel>) SessionManager.get("listSrvConditionModel");
		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/editservicecondition_view");

		for(int i=0;i<listtemp.size();i++){
			SrvConditionModel temp=listtemp.get(i);
			if(id.equals(temp.getId())){
				mav.addObject("model", temp);
				return mav;
			}
		}
		// qp.setSrvid(id);

		
		
		return mav;
	}
	

	
	/**
	 * 编辑服务时更新参数条件的数据
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/doEditConditionEdit", method = RequestMethod.POST)
	@ResponseBody
	public String doEditConditionEdit(SrvConditionModel m,HttpServletRequest request) {
		
		
		if (service.updateCondition(m) > 0) {
			return "success";
		} else{
			return "failure";
		}
		
		
		
	}
	
	
	

	/**
	 * 更新参数条件的数据
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/doEditCondition", method = RequestMethod.POST)
	@ResponseBody
	public String doEditCondition(SrvConditionModel model,HttpServletRequest request) {
		
		List<SrvConditionModel> listtemp=(List<SrvConditionModel>) SessionManager.get("listSrvConditionModel");
		boolean sessioncon=false;
		if(listtemp.size()>0){
			for(int i=0;i<listtemp.size();i++){
				SrvConditionModel temp=listtemp.get(i);
				if(temp.getId().equals(model.getId())){
					sessioncon=true;
					temp.setField_content(model.getField_content());
					temp.setParameter(model.getParameter());
					temp.setParameter_means(model.getParameter_means());
					temp.setRequired(model.getRequired());
					temp.setType(model.getType());
					temp.setSrvid(model.getSrvid());
					break;
				}
			}
			if(sessioncon){
				return "success";
			}else{
				return "failure";
			}
		}else{
			return "failure";
		}
				
	/*	if (service.updateCondition(model) > 0) {
			return "success";
		} else */
	}
	
	
	
	
	
	/**
	 * 预览服务中转到页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/doViewServiceTemp")
	@ResponseBody
	public ModelAndView doViewServiceTemp(HttpServletRequest request) {
		String srvid=request.getParameter("id");
		
		ModelAndView mav = new ModelAndView("/framework/interfaceservice/dataservice/serviceview");
		
		//查出条件参数
		List listsrvcondition = service.queryDataConditionService(srvid);
		List<String> parameterlist=new ArrayList<String>();
		List<String> parameterrequired=new ArrayList<String>();
		for (int i = 0; i < listsrvcondition.size(); i++){
			SrvConditionModel tempmodel = (SrvConditionModel) listsrvcondition.get(i);

			parameterlist.add(tempmodel.getParameter());
			parameterrequired.add(tempmodel.getRequired());
		}
		
		mav.addObject("parameterlist", parameterlist);
		mav.addObject("parameterrequired", parameterrequired);
		mav.addObject("srvid", srvid);
		
		return mav;
	}
	
	
	
	
	
	
	
	
	
	
	/**
	 * 预览服务
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/doViewService",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String doViewService(HttpServletRequest request) {
		
		String srvid=request.getParameter("srvid");
		String name=request.getParameter("p1");
		//找出原始的sql语句
		DataServiceModel qp=new DataServiceModel();
		qp.setSrvid(srvid);
		DataServiceModel model = (DataServiceModel) (service.getInfoById(qp));
		String basesql=model.getQuerysql();
		
		String datasource=model.getDatasource();
		//查出条件参数
		ConditionModel m=new ConditionModel();
		m.setSrvid(srvid);
	//	String consql=conParameter(m,request);
		
	//	String finsql=basesql+consql;
		//获得用户存的数据参数
		String str=request.getParameter("para");
		
		String tempsql=getFinSqlBefore(basesql,str);
		String fn=getFinSql(m,request,tempsql,str);
	//	String fn=getFinSql(m,request,tempsql);
		//IBaseDao dao=new BaseDao();
		//List resultJson =dao.queryObjectList("interface_service.Servicetemp", finsql);
		//List resultJson = service.queryPager(fn,"1","5");
		DynamicDataSource.setCustomerType(datasource);
		Map map=service.queryPagerP(fn,"1","5");
		DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCE);
		List list=(List) map.get("list");
		String total=(String) map.get("total");
		
		//JSONArray finJson = JSONArray.fromObject(resultJson);
		//预览的结果：json串
		//String res=DataGrideUtil.getDataGridJsonByList(resultJson);
		StringBuffer sb = new StringBuffer();
		if(list.size()>0){
			sb.append("{");
			sb.append("\"total\": "+total+",");
			sb.append("\"rows\": ");
			sb.append(CommonUtil.getJsonStringClob(list).toString());
			sb.append("}");
		}
		
		
		String f=sb.toString();
		JSONObject finJson = null;
		if(f.equals("")){
			sb.append("{");
			sb.append("\"total\": "+total+",");
			sb.append("\"rows\": ");
			sb.append("\"没有数据\"");
			sb.append("}");
			String f1=sb.toString();
			finJson = JSONObject.fromObject(f1);
		}else{
			finJson = JSONObject.fromObject(f);
			}
		
		return finJson.toString();
	}
	
	
	
	
	public String getFinSqlBefore(String tempsql1,String datajson){
		String tempsql= tempsql1;
		if(datajson!=null && !datajson.equals("")){
			Map map = (Map)JSONObject.toBean(JSONObject.fromObject(datajson), HashMap.class);
			Set set = map.entrySet();   
			Iterator i = set.iterator(); 
			int start=0;
			while(i.hasNext()){      
			     Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();   
			     String key=entry1.getKey();
			     String value=entry1.getValue();
			 	//判断是int还是String ,Int型（int）标识
			     int flag=key.indexOf("(int)");
			     boolean fl=false;
			     if(flag!=-1){
			    	 fl=true;
			    	 key=key.substring(0, flag);
			     }
			     
			   //  System.out.println(entry1.getKey()+"=="+entry1.getValue()); 
			     int index=tempsql.indexOf(key);
			     if(index!=-1){
			    
			    	start=tempsql.lastIndexOf("<c>", index); 
			    	String stconditionlabel=tempsql.substring(start, start+3);
					tempsql=tempsql.substring(0, start)+tempsql.substring(start+3,tempsql.length());
					 
					 
					int a=tempsql.indexOf(key);
					int b=tempsql.indexOf("</c>", a);
					 
					String lastconditionlabel=tempsql.substring(b, b+4);
					tempsql=tempsql.substring(0, b)+tempsql.substring(b+4,tempsql.length());
					
					if(fl){
						tempsql=tempsql.replaceAll("#"+key+"#", value);
					}else{
						//tempsql=tempsql.replaceAll("#"+key+"#", "'"+value+"'");
						tempsql=tempsql.replaceAll("#"+key+"#", value);
					}
					
			     }
			     
			} 
		}
		
		return tempsql;
	}
	
	
	
	
	
	public String getFinSql(ConditionModel m,HttpServletRequest request,String tempsql,String paratemp){
		List listsrvcondition = service.queryDataConditionService(m.getSrvid());
		//int start=tempsql.indexOf("<c>");
		int start=0;
		Map map=null;
		if(paratemp!=null){
			 map = (Map)JSONObject.toBean(JSONObject.fromObject(paratemp), HashMap.class);
		}else{
			// map = (Map)JSONObject.toBean(JSONObject.fromObject(""), HashMap.class);
		}

		for (int i = 0; i < listsrvcondition.size(); i++) {
			SrvConditionModel tempmodel = (SrvConditionModel) listsrvcondition.get(i);
			String flag=(String) map.get(tempmodel.getParameter());
			if(flag!=null){
				continue;
			}
			// 用户传来的参数的值
			String temp = null;
			String getorpost = request.getMethod();

			if (getorpost.equals("GET")) {
				String temp1 = null;
				String temp3 = null;
				temp1 = request.getParameter(tempmodel.getParameter());
				try {
					temp3 = new String(temp1.getBytes("ISO8859-1"), "UTF-8");
					temp = temp3;
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} else {
				temp = request.getParameter(tempmodel.getParameter());
			}
			
			
			if (temp != null&&!temp.equals("")) {
				if (tempmodel.getType().equals("1") ) {
					String para=tempmodel.getParameter();
					int index=tempsql.indexOf(para);
					start=tempsql.lastIndexOf("<c>", index);
					//找到前面的标签替换为空
					//int c1=tempsql.indexOf(">", start);
					String stconditionlabel=tempsql.substring(start, start+3);
					tempsql=tempsql.substring(0, start)+tempsql.substring(start+3,tempsql.length());
				//	tempsql=tempsql.replaceAll(stconditionlabel, "");
					
					
					//找到后面的标签替换为空
					int a=tempsql.indexOf(para);
					int b=tempsql.indexOf("</c>", a);
					//int c=tempsql.indexOf(">", b);
					
					String lastconditionlabel=tempsql.substring(b, b+4);
				//	tempsql=tempsql.replaceAll(lastconditionlabel, "");
					tempsql=tempsql.substring(0, b)+tempsql.substring(b+4,tempsql.length());

					tempsql=tempsql.replaceAll("#"+para+"#", temp);
				//	tempsql=tempsql.replaceAll("#"+para+"#", "'"+temp+"'");
				//	int a11=tempsql.indexOf("'"+temp+"'");
				//	int b11=tempsql.indexOf("<c>", a11);
					
					
					//start=b11;
				} else {
					String para=tempmodel.getParameter();
					int index=tempsql.indexOf(para);
					start=tempsql.lastIndexOf("<c>", index);
					//找到前面的标签替换为空
					//int c1=tempsql.indexOf(">", start);
					String stconditionlabel=tempsql.substring(start, start+3);
					//tempsql=tempsql.replaceAll(stconditionlabel, "");
					tempsql=tempsql.substring(0, start)+tempsql.substring(start+3,tempsql.length());

					
					//找到后面的标签替换为空
					int a=tempsql.indexOf(para);
					int b=tempsql.indexOf("</c>", a);
					//int c=tempsql.indexOf(">", b);
					
					
					String lastconditionlabel=tempsql.substring(b, b+4);
				//	tempsql=tempsql.replaceAll(lastconditionlabel, "");
					tempsql=tempsql.substring(0, b)+tempsql.substring(b+4,tempsql.length());

					tempsql=tempsql.replaceAll("#"+para+"#", temp);
				//	int a11=tempsql.indexOf("'"+temp+"'");
				//	int b11=tempsql.indexOf("<c>", a11);
					
					//start=b11;
				}
			} else {
				// 必传参数为空
				if (tempmodel.getRequired().equals("1")) {
					return null;
				}else{

					String para=tempmodel.getParameter();
					int index=tempsql.indexOf(para);
					start=tempsql.lastIndexOf("<c>", index);
					//找到前面的标签替换为空
					int a=tempsql.indexOf(para);
					int c=tempsql.indexOf("</c>", a);
					String str=tempsql.substring(start, c+4);
					
					
					//tempsql=tempsql.replaceAll(str, "");
					tempsql=tempsql.substring(0, start)+tempsql.substring(c+4,tempsql.length());

					
					//start=tempsql.indexOf("<", c11);
					
				}
			}
			
			
			
		}
		return tempsql;
	}
	
	
	public String getFinSql1(ConditionModel m,HttpServletRequest request,String tempsql){
		List listsrvcondition = service.queryDataConditionService(m.getSrvid());
		int start=tempsql.indexOf("<");
		
		for (int i = 0; i < listsrvcondition.size(); i++) {
			SrvConditionModel tempmodel = (SrvConditionModel) listsrvcondition.get(i);
			// 用户传来的参数的值
			String temp = null;
			String getorpost = request.getMethod();

			if (getorpost.equals("GET")) {
				String temp1 = null;
				String temp2 = null;
				String temp3 = null;
				temp1 = request.getParameter(tempmodel.getParameter());
				try {
					temp2 = new String(temp1.getBytes("ISO8859-1"));
					temp3 = new String(temp1.getBytes("ISO8859-1"), "UTF-8");
					temp = temp3;
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} else {
				temp = request.getParameter(tempmodel.getParameter());
			}
			
			
			if (temp != null&&!temp.equals("")) {
				if (tempmodel.getType().equals("1") ) {
					String para=tempmodel.getParameter();
					
					//找到前面的标签替换为空
					int c1=tempsql.indexOf(">", start);
					String stconditionlabel=tempsql.substring(start, c1+1);
					tempsql=tempsql.replaceAll(stconditionlabel, "");
					
					
					//找到后面的标签替换为空
					int a=tempsql.indexOf(para);
					int b=tempsql.indexOf("</", a);
					int c=tempsql.indexOf(">", b);
					
					String lastconditionlabel=tempsql.substring(b, c+1);
					tempsql=tempsql.replaceAll(lastconditionlabel, "");
					
					tempsql=tempsql.replaceAll("#"+para+"#",temp);
				//	tempsql=tempsql.replaceAll("#"+para+"#", "'"+temp+"'");
//					
					int a11=tempsql.indexOf("'"+temp+"'");
					int b11=tempsql.indexOf("<", a11);
					
					
					start=b11;
				} else {
					String para=tempmodel.getParameter();
					
					//找到前面的标签替换为空
					int c1=tempsql.indexOf(">", start);
					String stconditionlabel=tempsql.substring(start, c1+1);
					tempsql=tempsql.replaceAll(stconditionlabel, "");
					
					
					//找到后面的标签替换为空
					int a=tempsql.indexOf(para);
					int b=tempsql.indexOf("</", a);
					int c=tempsql.indexOf(">", b);
					
					
					String lastconditionlabel=tempsql.substring(b, c+1);
					tempsql=tempsql.replaceAll(lastconditionlabel, "");
					tempsql=tempsql.replaceAll("#"+para+"#", temp);
					int a11=tempsql.indexOf("'"+temp+"'");
					int b11=tempsql.indexOf("</", a11);
					
					start=b11;
				}
			} else {
				// 必传参数为空
				if (tempmodel.getRequired().equals("1")) {
					return null;
				}else{

					String para=tempmodel.getParameter();
					
					//找到前面的标签替换为空
					int a=tempsql.indexOf(para);
					int c=tempsql.indexOf(">", a);
					String str=tempsql.substring(start, c+1);
					
					
					tempsql=tempsql.replaceAll(str, "");
					
					
					//start=tempsql.indexOf("<", c11);
					
				}
			}
			
			
			
		}
		return tempsql;
	}
	
	
	
	
	
	public String conParameter(ConditionModel m,HttpServletRequest request){
		// 查库，找出srvid服务传入的参数
		List listsrvcondition = service.queryDataConditionService(m.getSrvid());

		/*// 将数据库中相应服务所需的参数找出来
		//List<String> conditionParameter = new ArrayList<String>();
		// 参数对应数据库中的字段内容
		List<String> field_content = new ArrayList<String>();

		// 记录用户传来的条件参数
		List<String> conditionParameterres = new ArrayList<String>();*/

		// 拼接sql条件
		String conditionsql = "";
		for (int i = 0; i < listsrvcondition.size(); i++) {
			SrvConditionModel tempmodel = (SrvConditionModel) listsrvcondition.get(i);
			// 用户传来的参数的值
			String temp = request.getParameter(tempmodel.getParameter());
			// 如果用户传来的参数不为空
			if (!temp.equals("")) {
				if (tempmodel.getType() == "0") {
					// 类型为int型
					conditionsql = conditionsql + "and ";
					conditionsql = conditionsql + tempmodel.getField_content() + "=" + temp;
				} else {
					conditionsql = conditionsql + "and ";
					conditionsql = conditionsql + tempmodel.getField_content() + "=" + "'" + temp + "'";
				}
			} else {
				// 必传参数为空
				if (tempmodel.getRequired() == "1") {
					return null;
				}
			}
			
		}
		// 去掉最后的and
	/*	int end = conditionsql.length() - 4;
		conditionsql = conditionsql.substring(0, end);*/

		return conditionsql;
	}
	
	
	
	
	/**
	 * 确认sql是否正确
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/sqlConfirm")
	@ResponseBody
	public String sqlConfirm(String querysql,String srvname,String srvid) {
		DataServiceModel ds=new DataServiceModel();
		ds.setSrvname(srvname);
		
//		int a=querysql.indexOf("where");
//		if(a!=-1){
//			querysql=querysql.substring(0, a);
//		}
		
		
		if(srvid!=null){
			ds.setSrvid(srvid);
		}
		//DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCEINTERFACE);
		int count=service.namelimit(ds);
		//DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCE);

		if(count>0){
			return "samename";
		}else{
//			DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCEINTERFACE);
			DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCE);
			return "success";
		}
		
		
	}
	
	
	
	
	
	/**
	 * 计算校验位checkbit
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getCheckBit")
	@ResponseBody
	public String getCheckBit(HttpServletRequest request) {
		String userid=request.getParameter("userid");
		String srvid=request.getParameter("srvid");
		String timeStamp=request.getParameter("timeStamp");
		String userlogname=request.getParameter("userlogname");
		String str=userlogname+srvid+timeStamp;
		//得到私钥
		UserInfoModel userinfomodel = service.findUserInfo(userid);
		String privatekey = userinfomodel.getPrivatekey();
		
		str+=privatekey;
		
		String md5string = "";
		// 对connectparameter进行MD5加密
		try {

			md5string = MD5Helper.encrypt(str);
			// md5string1=MD5Helper.encryptTemp(connectparameter,"rre");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return md5string;
	}
	
	
	
	/**
	 * 计算当前时间
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/getCurrentTime")
	@ResponseBody
	public String getCurrentTime(HttpServletRequest request) {
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String currenttime = format.format(date);
		return currenttime;
	}
	
	
	@RequestMapping(value = "/eer")
	public String eer(HttpServletRequest request) {
		return "test/block-list";
	}
	
}